##    This file is part of The Most Awesome Story Ever
##
##    Foobar is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    Foobar is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
##    
##    Copyright 2008, Ryan Michela

import wsgiref.handlers
import pagemodel
import images

from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from google.appengine.ext import db 



class MainHandler(webapp.RequestHandler):
  """This class bootstraps the Page database with the first page text and image."""

  def get(self):
    """Process the HTTP GET verb; load the initial database record."""
    
    aPage = pagemodel.Page(pageNum = 1)
    
    aPage.title = 'You Are Free!'
    
    aPage.body = """<div style="text-align: center; font-style: italic;">Welcome 
    to my social experiment -- 
    a user-generated choose your own adventure story. Please contribute to make this 
    the most awesome story ever written.</div>
    <hr>
    <p>You stand amidst the ramparts of an island fort, overlooking the harbor. The 
    sky is clear and a deep blue and a warm breeze crosses your face. Along the horizon 
    you see more islands peeking up over the waves. You try to remember the last time 
    you felt the breeze, but you can't. Three years in that prison cell changes a person -- 
    strips away memories until all that's left is an empty shell and fleeting glint 
    of familiarity.</p>
    <p>You pause for just a moment. Linger too long and one of the cameras will spot 
    you. On the breeze you smell the feint odor of cooking flesh. Most likely the 
    natives back at their camp, celebrating a successful hunt. The women are singing 
    their tribal songs. Looking over your shoulder, you see the research compound you 
    were assigned to for the past five years. The satellite dish points skyward like 
    a metallic, inverted mushroom. Muffled voices can be heard through the window 
    you broke to win your freedom. You can't remember the taste of meat.</p>
    <p>Suddenly, your attention is focused by the sound of an arrow ricocheting off 
    the concrete barrier to your right. A saddled pterodactyl screeches as it flies 
    overhead.</p>
    <hr>
    <div style="text-align: center; font-style: italic;"><a href="/unfinishedendings.html">
    For a good place to start editing, <span style="text-decoration: underline;">
    click here</span> to see a list of unfinished endings.</a></div>"""
    
    #initialize the image
    url = "http://mostawesomesotry.appspot.com/images/island.jpg"
    result = urlfetch.fetch(url)
    if result.status_code == 200:
        imageTouple = images.prepareImage(result.content)
        aPage.imageExtension = imageTouple[0]
        aPage.image = db.Blob(imageTouple[1])
        
        aPage.put()

        self.response.out.write("""<html>
                                    <body>
                                        DB loaded<br>
                                        <a href="/page1.html">Got to page 1</a>
                                    </body>
                                   </html>""")
    else:
        self.response.out.write("Error loading image: %s" % result.status_code)

def main():
  application = webapp.WSGIApplication([('/dbinit.html', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()